//
//  FruitDetailViewController.m
//  FruitHelper
//
//  Created by 赵隆杰 on 2021/12/11.
//

#import "FruitDetailViewController.h"

@interface FruitDetailViewController ()

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *addTimeLabel;
@property (nonatomic, strong) UILabel *finishTimeLabel;
@property (nonatomic, strong) UILabel *weightLabel;

@end

@implementation FruitDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"详情";
    
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.backgroundColor = kLineColor;
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.layer.cornerRadius = 5;
    imageView.layer.masksToBounds = YES;
    [self.view addSubview:imageView];
    self.imageView = imageView;
    [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.width.height.equalTo(@100);
        make.top.equalTo(self.view).offset(150);
    }];

    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:titleLabel];
    self.titleLabel = titleLabel;
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(_imageView.mas_bottom).offset(10);
        make.height.equalTo(@40);
    }];

    UILabel *addTimeLabel = [[UILabel alloc] init];
    addTimeLabel.textColor = kColor333333;
    addTimeLabel.font = [UIFont systemFontOfSize:12];
    [self.view addSubview:addTimeLabel];
    self.addTimeLabel = addTimeLabel;
    [_addTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@20);
        make.top.equalTo(_titleLabel.mas_bottom).offset(10);
        make.height.equalTo(@40);
    }];

    UILabel *finishTimeLabel = [[UILabel alloc] init];
    finishTimeLabel.textColor = kColor333333;
    finishTimeLabel.font = [UIFont systemFontOfSize:12];
    [self.view addSubview:finishTimeLabel];
    self.finishTimeLabel = finishTimeLabel;
    [_finishTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@20);
        make.top.equalTo(_addTimeLabel.mas_bottom).offset(10);
        make.height.equalTo(@40);
    }];

    UILabel *weightLabel = [[UILabel alloc] init];
    weightLabel.textColor = kColor333333;
    weightLabel.font = [UIFont systemFontOfSize:12];
    [self.view addSubview:weightLabel];
    self.weightLabel = weightLabel;
    [_weightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@20);
        make.top.equalTo(_finishTimeLabel.mas_bottom).offset(10);
        make.height.equalTo(@40);
    }];

    UIButton *shareButton = [[UIButton alloc] init];
    shareButton.layer.cornerRadius = 20;
    shareButton.layer.masksToBounds = YES;
    shareButton.backgroundColor = kThemeColor;
    shareButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [shareButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [shareButton setTitle:@"分享" forState:UIControlStateNormal];
    [shareButton addTarget:self action:@selector(shareBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:shareButton];
    [shareButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@20);
        make.height.equalTo(@40);
        make.right.equalTo(self.view.mas_centerX).offset(-10);
        make.top.equalTo(self.view.mas_bottom).offset(-100);
    }];

    UIButton *deleteButton = [[UIButton alloc] init];
    deleteButton.layer.cornerRadius = 20;
    deleteButton.layer.masksToBounds = YES;
    deleteButton.backgroundColor = kThemeColor;
    deleteButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [deleteButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [deleteButton setTitle:@"删除" forState:UIControlStateNormal];
    [deleteButton addTarget:self action:@selector(deleteBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:deleteButton];
    [deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@-20);
        make.height.equalTo(@40);
        make.left.equalTo(self.view.mas_centerX).offset(10);
        make.top.equalTo(self.view.mas_bottom).offset(-100);
    }];

    if (_model.imageType == 1) {
        UIImage *image = [NSObject Base64StrToUIImage:_model.imageStr];
        self.imageView.image = image;
    } else {
        self.imageView.image = [UIImage imageNamed:_model.imageStr];
    }
    self.titleLabel.text = [NSString stringWithFormat:@"%@", _model.title];
    NSString *addTimeStr = [NSDate timeStringToInterval:_model.addTime.doubleValue dataFormat:@"yyyy-MM-dd HH:mm"];
    self.addTimeLabel.text = [NSString stringWithFormat:@"添加时间：%@", addTimeStr];
    NSString *finishTimeStr = [NSDate timeStringToInterval:_model.addTime.doubleValue dataFormat:@"yyyy-MM-dd HH:mm"];
    self.finishTimeLabel.text = [NSString stringWithFormat:@"完成时间：%@", finishTimeStr];
    self.weightLabel.text = [NSString stringWithFormat:@"重量：%@KG", _model.kg];
}

- (void)setModel:(FruitModel *)model
{
    _model = model;
}

- (void)shareBtnClick
{
    //1.设定分享的内容,比如:
    NSString *shareTitle = _model.title;

    UIImage *shareImage = nil;
    if (_model.imageType == 1) {
        shareImage = [NSObject Base64StrToUIImage:_model.imageStr];
    } else {
        shareImage = [UIImage imageNamed:_model.imageStr];
    }
     
    NSArray *activityItems = @[shareTitle, shareImage]; // 必须要提供url 才会显示分享标签否则只显示图片

    //2.创建分享的控制器
    UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:nil];
    
    //3.设定不想显示的平台和功能
//    activityVC.excludedActivityTypes = [self excludetypes];

    //4. 设置操作回调,用户点击 菜单按钮后事件执行完成会回调这个block
    activityVC.completionWithItemsHandler = ^(UIActivityType  _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) {

            if (completed) {
                NSLog(@"分享成功");
            }else{
                NSLog(@"分享失败");
            }
        };

    //5.显示分享菜单
    [self presentViewController:activityVC animated:YES completion:nil];

    

    
}
- (void)deleteBtnClick
{
    VHWeakSelf
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"删除后不保存记录" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        VHStrongSelf
        [LocalData deleteFinishFruit:self.model.vege_id];
        [self.navigationController popViewControllerAnimated:YES];
        [kAppWindow makeToast:@"已删除！" duration:2 position:CSToastPositionTop];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];

    [alertController addAction:deleteAction];
    [alertController addAction:cancelAction];

    UIViewController *vc = [UIWindow currentViewController];
    [vc presentViewController:alertController animated:YES completion:nil];
    
}

-(NSArray *)excludetypes{

    NSMutableArray *excludeTypeM = [NSMutableArray arrayWithArray:@[//UIActivityTypePostToFacebook,

    UIActivityTypePostToTwitter,

    UIActivityTypePostToWeibo,

    UIActivityTypeMessage,

    UIActivityTypeMail,

    UIActivityTypePrint,

    UIActivityTypeCopyToPasteboard,

    UIActivityTypeAssignToContact,

    UIActivityTypeSaveToCameraRoll,

    UIActivityTypeAddToReadingList,

    UIActivityTypePostToFlickr,

    UIActivityTypePostToVimeo,

    UIActivityTypePostToTencentWeibo,

    UIActivityTypeAirDrop,

    UIActivityTypeOpenInIBooks]];

    if (@available(iOS 11.0, *)) {

        [excludeTypeM addObject:UIActivityTypeMarkupAsPDF];

    } else {

        // Fallback on earlier versions

    }

    return excludeTypeM;

}

@end
